package jp.co.piratealliance.pirateAllianceWeb.web.action.com;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import jp.co.piratealliance.pirateAllianceWeb.common.base.BaseAction;
import jp.co.piratealliance.pirateAllianceWeb.def.Constants;
import jp.co.piratealliance.pirateAllianceWeb.def.MessageCodeConstants;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.UserInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.dto.com.UserLanguageInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.helper.CommonHelper;
import jp.co.piratealliance.pirateAllianceWeb.service.com.UserService;

/**
 * copyright pirate alliance team 2014
 *
 * @author zhangjie
 */

/**
 * @author panxuejuan
 *
 */
@SuppressWarnings("serial")
public class UserRegisterWebAction extends BaseAction {
	private UserService userService;

	private UserInfoDto userInfoDto;
	
	private List<UserInfoDto> userList;

	private String email;

	private String password;
	
	private String passwordSure;

	private String userId;

	private String shopId;

	private String goldenCoinId;

	private String silverCoinId;
	private String accountId;
	private String nameKanJiSei;
	private String nameKanJiMei;
	private String nameKatakanaSei;
	private String nameKatakanaMei;
	private String nameEnFamily;
	private String nameEnMiddle;

	private String nameEnLast;
	private String birthday;
	private String selYear;
	private String selMonth;
	private String selDay;

	private String tel;
	private int status;
	private String createTime;
	private String loginTime;

	private String lastloginTime;
	private int sex;

	private String updateTime;
	private int loginCount;
	private String icon;

    private String countryId;

//	private String selfIntroduction;

	private String zipCode;

	private String addProvince;

	private String addCity;

	private String addOther;
	
	private String userForModify;
	
	private boolean checkFlag;

	public String doInit(){
//		String userId="2014082500000005";
//		userList = userService.getUserListInfo(userId);
//		if(userList != null && userList.size() > 0){
//			setUserListInfo(userList);//用户信息一览
//		}
		return SUCCESS;
	}
	
	//用户信息修改
	public String updateUserInit(){
		String userid=ServletActionContext.getRequest().getParameter("userid");
      this.userInfoDto=this.userService.getServiceUserInfo(userid);
      setUserListInfo(userInfoDto);
      this.userForModify="1";
		return  SUCCESS;
	}

	//用户注册
	public String registerUser()
	{
		boolean isSuccess=true;
		if(password != null && passwordSure !=null && !password.equals(passwordSure)){
			this.addFieldError("passwordSure",MessageCodeConstants.E_00000003);
			isSuccess=false;
		}
//		if(selfIntroduction.length()>180){
//			this.addFieldError("selfIntroduction", getText("validation0010"));
//			isSuccess=false;
//		}
		if((email.length()>80)||(password.length()>80)||(nameKanJiSei.length()>30)||(nameKanJiSei.length()>30)
				||(nameKanJiMei.length()>30)||(nameKatakanaMei.length()>30)||(nameKatakanaSei.length()>30)
				||(nameEnMiddle.length()>30)||(nameEnFamily.length()>30)||(nameEnLast.length()>30)
				||(addCity.length()>80)||(addProvince.length()>80)||(addOther.length()>80)
				){
			isSuccess=false;
			this.addFieldError("textOverLimit", getText("validation0011"));
		}
			
		if(isSuccess){
			UserInfoDto dto = new UserInfoDto();
			UserLanguageInfoDto userLanguageInfo =new UserLanguageInfoDto();
			String userIdSeq = CommonHelper.getSequence(Constants.TB_M_USER);
			dto.setUser_id(userIdSeq);
			userLanguageInfo.setUser_id(userIdSeq);
			//TODO 临时对应userLanguageInfo中其他字段均未设定
			
			String goldenCoinIdSeq = CommonHelper.getSequence(Constants.TB_GOLDEN_COIN);
			String silverCoinIdSeq = CommonHelper.getSequence(Constants.TB_SILVER_COIN);

			dto.setGolden_coin_id(goldenCoinIdSeq);
			dto.setSilver_coin_id(silverCoinIdSeq);

			//口座ID
			dto.setAccount_id(accountId);
			//国ID
			dto.setCountry_id(countryId);

			//日文姓名相关字段
			dto.setName_kanji_sei(nameKanJiSei);
			dto.setName_kanji_mei(nameKanJiMei);
			dto.setName_katakana_sei(nameKatakanaSei);
			dto.setName_katakana_mei(nameKatakanaMei);
			//英文姓名相关字段
			dto.setName_en_family(nameEnFamily);
			dto.setName_en_middle(nameEnMiddle);
			dto.setName_en_last(nameEnLast);
			//生日
			//		birthday =selYear+"-"+selMonth+"-"+selDay;
			dto.setBirthday(birthday);
			//email address
			dto.setEmail(email);
			//電話番号
			dto.setTel(tel);
			//password
			dto.setPassword(password);
			//status为【1:有效】
			dto.setStatus(Constants.STATUS_ACTIVE);
			//login次数
			dto.setLogin_count(0);
			//ユーザーのアイコン
			dto.setIcon("default_face.jpg");
			//性別(0:男性-default, 1:女性,2:その他)
			dto.setSex(sex);

			//自己紹介
//			dto.setSelf_introduction(selfIntroduction);
			//郵便番号
			dto.setZip_code(zipCode);
			//都道府県
			dto.setAdd_province(addProvince);
			//市区町村
			dto.setAdd_city(addCity);
			//以降の住所
			dto.setAdd_other(addOther);

			userInfoDto = userService.userRegister(dto, userLanguageInfo);
			return SUCCESS;
		}
		return INPUT;
	}

	//用户信息修改
	public String updateUser()
	{    
		boolean isSuccess=true;
		if(password != null && passwordSure !=null && !password.equals(passwordSure)){
			this.addFieldError("passwordSure",MessageCodeConstants.E_00000003);
			isSuccess=false;
		}
//		if(selfIntroduction.length()>180){
//			this.addFieldError("selfIntroduction", getText("validation0010"));
//			isSuccess=false;
//		}
		
		if((email.length()>80)||(password.length()>80)||(nameKanJiSei.length()>30)||(nameKanJiSei.length()>30)
				||(nameKanJiMei.length()>30)||(nameKatakanaMei.length()>30)||(nameKatakanaSei.length()>30)
				||(nameEnMiddle.length()>30)||(nameEnFamily.length()>30)||(nameEnLast.length()>30)
				||(addCity.length()>80)||(addProvince.length()>80)||(addOther.length()>80)
				){
			isSuccess=false;
			this.addFieldError("textOverLimit", getText("validation0012"));
		}
		if(isSuccess){
		UserInfoDto dto = new UserInfoDto();
		UserLanguageInfoDto userLanguageInfo =new UserLanguageInfoDto();
		dto.setUser_id(userId);
		userLanguageInfo.setUser_id(userId);
		//TODO 临时对应userLanguageInfo中其他字段均未设定
		
		//口座ID
		dto.setAccount_id(accountId);
		//国ID
		dto.setCountry_id(countryId);

		//日文姓名相关字段
		dto.setName_kanji_sei(nameKanJiSei);
		dto.setName_kanji_mei(nameKanJiMei);
		dto.setName_katakana_sei(nameKatakanaSei);
		dto.setName_katakana_mei(nameKatakanaMei);
		//英文姓名相关字段
		dto.setName_en_family(nameEnFamily);
		dto.setName_en_middle(nameEnMiddle);
		dto.setName_en_last(nameEnLast);
		//生日
//		birthday =selYear+"-"+selMonth+"-"+selDay;
		dto.setBirthday(birthday);
		//email address
		dto.setEmail(email);
		//電話番号
		dto.setTel(tel);
		//password
		dto.setPassword(password);
		//status为【1:有效】
		dto.setStatus(Constants.STATUS_ACTIVE);
		//login次数
		dto.setLogin_count(1);
		//ユーザーのアイコン
		dto.setIcon("default_face.jpg");
		//性別(0:男性-default, 1:女性,2:その他)
		dto.setSex(sex);
		//自己紹介
//		dto.setSelf_introduction(selfIntroduction);
		//郵便番号
		dto.setZip_code(zipCode);
		//都道府県
		dto.setAdd_province(addProvince);
		//市区町村
		dto.setAdd_city(addCity);
		//以降の住所
		dto.setAdd_other(addOther);
		userService.updateUserInfo(dto,userLanguageInfo);
	    ActionContext.getContext().getSession().remove("user");
		return SUCCESS;
		}
		this.userForModify="1";
		return INPUT;
	}
	
	private void setUserListInfo(UserInfoDto userInfo) {

			userId=userInfo.getUser_id();
			nameKanJiSei =userInfo.getName_kanji_sei();
			nameKanJiMei=userInfo.getName_kanji_mei();
			nameKatakanaSei=userInfo.getName_katakana_sei();
			nameKatakanaMei=userInfo.getName_katakana_mei();
			nameEnFamily=userInfo.getName_en_family();
			nameEnMiddle=userInfo.getName_en_middle();
			nameEnLast=userInfo.getName_en_last();
			String [] birthdayArray=userInfo.getBirthday().split("-");
			selYear = birthdayArray[0];
			selMonth = birthdayArray[1];
			selDay = birthdayArray[2];
			birthday=userInfo.getBirthday();
			email=userInfo.getEmail();
			tel=userInfo.getTel();
			password=userInfo.getPassword();
			passwordSure=userInfo.getPassword();
			sex=userInfo.getSex();
//			selfIntroduction=userInfo.getSelf_introduction();
			zipCode=userInfo.getZip_code();
			addProvince=userInfo.getAdd_province();
			addCity=userInfo.getAdd_city();
			addOther=userInfo.getAdd_other();
			loginTime=userInfo.getLogin_time();
			lastloginTime=userInfo.getLast_login_time();
		}
	
	
	public String checkMultiEmail(){
		String email = ServletActionContext.getRequest().getParameter("email");
		this.checkFlag =userService.checkEmailMult(email);
		return SUCCESS;
	}

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the userInfoDto
	 */
	public UserInfoDto getUserInfoDto() {
		return userInfoDto;
	}

	/**
	 * @param userInfoDto the userInfoDto to set
	 */
	public void setUserInfoDto(UserInfoDto userInfoDto) {
		this.userInfoDto = userInfoDto;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the userId
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * @param userId the userId to set
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * @return the shopId
	 */
	public String getShopId() {
		return shopId;
	}

	/**
	 * @param shopId the shopId to set
	 */
	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	/**
	 * @return the goldenCoinId
	 */
	public String getGoldenCoinId() {
		return goldenCoinId;
	}

	/**
	 * @param goldenCoinId the goldenCoinId to set
	 */
	public void setGoldenCoinId(String goldenCoinId) {
		this.goldenCoinId = goldenCoinId;
	}

	/**
	 * @return the silverCoinId
	 */
	public String getSilverCoinId() {
		return silverCoinId;
	}

	/**
	 * @param silverCoinId the silverCoinId to set
	 */
	public void setSilverCoinId(String silverCoinId) {
		this.silverCoinId = silverCoinId;
	}

	/**
	 * @return the accountId
	 */
	public String getAccountId() {
		return accountId;
	}

	/**
	 * @param accountId the accountId to set
	 */
	public void setAccountId(String accountId) {
		this.accountId = accountId;
	}

	/**
	 * @return the nameKanJiSei
	 */
	public String getNameKanJiSei() {
		return nameKanJiSei;
	}

	/**
	 * @param nameKanJiSei the nameKanJiSei to set
	 */
	public void setNameKanJiSei(String nameKanJiSei) {
		this.nameKanJiSei = nameKanJiSei;
	}

	/**
	 * @return the nameKanJiMei
	 */
	public String getNameKanJiMei() {
		return nameKanJiMei;
	}

	/**
	 * @param nameKanJiMei the nameKanJiMei to set
	 */
	public void setNameKanJiMei(String nameKanJiMei) {
		this.nameKanJiMei = nameKanJiMei;
	}

	/**
	 * @return the nameKatakanaSei
	 */
	public String getNameKatakanaSei() {
		return nameKatakanaSei;
	}

	/**
	 * @param nameKatakanaSei the nameKatakanaSei to set
	 */
	public void setNameKatakanaSei(String nameKatakanaSei) {
		this.nameKatakanaSei = nameKatakanaSei;
	}

	/**
	 * @return the nameKatakanaMei
	 */
	public String getNameKatakanaMei() {
		return nameKatakanaMei;
	}

	/**
	 * @param nameKatakanaMei the nameKatakanaMei to set
	 */
	public void setNameKatakanaMei(String nameKatakanaMei) {
		this.nameKatakanaMei = nameKatakanaMei;
	}

	/**
	 * @return the nameEnFamily
	 */
	public String getNameEnFamily() {
		return nameEnFamily;
	}

	/**
	 * @param nameEnFamily the nameEnFamily to set
	 */
	public void setNameEnFamily(String nameEnFamily) {
		this.nameEnFamily = nameEnFamily;
	}

	/**
	 * @return the nameEnMiddle
	 */
	public String getNameEnMiddle() {
		return nameEnMiddle;
	}

	/**
	 * @param nameEnMiddle the nameEnMiddle to set
	 */
	public void setNameEnMiddle(String nameEnMiddle) {
		this.nameEnMiddle = nameEnMiddle;
	}

	/**
	 * @return the nameEnLast
	 */
	public String getNameEnLast() {
		return nameEnLast;
	}

	/**
	 * @param nameEnLast the nameEnLast to set
	 */
	public void setNameEnLast(String nameEnLast) {
		this.nameEnLast = nameEnLast;
	}

	/**
	 * @return the birthday
	 */
	public String getBirthday() {
		return birthday;
	}

	/**
	 * @param birthday the birthday to set
	 */
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	/**
	 * @return the tel
	 */
	public String getTel() {
		return tel;
	}

	/**
	 * @param tel the tel to set
	 */
	public void setTel(String tel) {
		this.tel = tel;
	}

	/**
	 * @return the status
	 */
	public int getStatus() {
		return status;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(int status) {
		this.status = status;
	}

	/**
	 * @return the createTime
	 */
	public String getCreateTime() {
		return createTime;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	/**
	 * @return the loginTime
	 */
	public String getLoginTime() {
		return loginTime;
	}

	/**
	 * @param loginTime the loginTime to set
	 */
	public void setLoginTime(String loginTime) {
		this.loginTime = loginTime;
	}

	/**
	 * @return the lastloginTime
	 */
	public String getLastloginTime() {
		return lastloginTime;
	}

	/**
	 * @param lastloginTime the lastloginTime to set
	 */
	public void setLastloginTime(String lastloginTime) {
		this.lastloginTime = lastloginTime;
	}

	/**
	 * @return the updateTime
	 */
	public String getUpdateTime() {
		return updateTime;
	}

	/**
	 * @param updateTime the updateTime to set
	 */
	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}

	/**
	 * @return the loginCount
	 */
	public int getLoginCount() {
		return loginCount;
	}

	/**
	 * @param loginCount the loginCount to set
	 */
	public void setLoginCount(int loginCount) {
		this.loginCount = loginCount;
	}

	/**
	 * @return the icon
	 */
	public String getIcon() {
		return icon;
	}

	/**
	 * @param icon the icon to set
	 */
	public void setIcon(String icon) {
		this.icon = icon;
	}
	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getCountryId() {
		return countryId;
	}

	public void setCountryId(String countryId) {
		this.countryId = countryId;
	}

//	public String getSelfIntroduction() {
//		return selfIntroduction;
//	}
//
//	public void setSelfIntroduction(String selfIntroduction) {
//		this.selfIntroduction = selfIntroduction;
//	}

	public String getAddCity() {
		return addCity;
	}

	public void setAddCity(String addCity) {
		this.addCity = addCity;
	}

	public String getAddOther() {
		return addOther;
	}

	public void setAddOther(String addOther) {
		this.addOther = addOther;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getAddProvince() {
		return addProvince;
	}

	public void setAddProvince(String addProvince) {
		this.addProvince = addProvince;
	}

	public String getSelYear() {
		return selYear;
	}

	public void setSelYear(String selYear) {
		this.selYear = selYear;
	}

	public String getSelMonth() {
		return selMonth;
	}

	public void setSelMonth(String selMonth) {
		this.selMonth = selMonth;
	}

	public String getSelDay() {
		return selDay;
	}

	public void setSelDay(String selDay) {
		this.selDay = selDay;
	}

	public String getUserForModify() {
		return userForModify;
	}

	public void setUserForModify(String userForModify) {
		this.userForModify = userForModify;
	}

	public List<UserInfoDto> getUserList() {
		return userList;
	}

	public void setUserList(List<UserInfoDto> userList) {
		this.userList = userList;
	}

	public String getPasswordSure() {
		return passwordSure;
	}

	public void setPasswordSure(String passwordSure) {
		this.passwordSure = passwordSure;
	}

	public boolean isCheckFlag() {
		return checkFlag;
	}

	public void setCheckFlag(boolean checkFlag) {
		this.checkFlag = checkFlag;
	}

}
